package bb.lanxing.adapter.team;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.model.database.EventApproveUser;

public class TeamApprovalAdapter extends RecyclerView.Adapter<TeamApprovalAdapter.ViewHolder> {
    private List<EventApproveUser> list = new ArrayList<>();
    private PassClickListener passClickListener;
    private NotPassClickListener notPassClickListener;
    private Context mContext;

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView avatar;
        TextView name;
        ImageView pass;
        ImageView not_pass;
        TextView status;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.avatar);
            name = itemView.findViewById(R.id.name);
            pass = itemView.findViewById(R.id.pass);
            not_pass = itemView.findViewById(R.id.not_pass);
            status = itemView.findViewById(R.id.status);
        }
    }

    public interface PassClickListener {
        void onItemClick(int position);
    }

    public interface NotPassClickListener {
        void onItemClick(int position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_team_approval, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        EventApproveUser eventApproveUser = list.get(position);
        if (eventApproveUser != null) {
            Glide.with(mContext).load(eventApproveUser.avatar).transform(new RoundedCorners(100)).into(holder.avatar);
            holder.name.setText(eventApproveUser.nickName);
            holder.pass.setOnClickListener(v -> passClickListener.onItemClick(position));
            holder.not_pass.setOnClickListener(v -> notPassClickListener.onItemClick(position));
            if (eventApproveUser.status != null) {
                holder.pass.setVisibility(View.GONE);
                holder.not_pass.setVisibility(View.GONE);
                holder.status.setVisibility(View.VISIBLE);
                holder.status.setText(eventApproveUser.status);
                if ("审核通过".equals(eventApproveUser.status)) {
                    holder.status.setTextColor(mContext.getColor(R.color.green));
                } else {
                    holder.status.setTextColor(mContext.getColor(R.color.red));
                }
            }

        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public void setPassClickListener(PassClickListener listener) {
        passClickListener = listener;
    }

    public void setNotPassClickListener(NotPassClickListener listener) {
        notPassClickListener = listener;
    }

    public void updateData(List<EventApproveUser> dataList) {
        list = dataList;
        notifyDataSetChanged();
    }
}